package winOrDie.GameEngine.RT.Server_Engine;

import java.util.ArrayList;

import winOrDie.Net.RT.WoD_RT_Client_Movement;
import winOrDie.Net.Server.WoD_Server_Client;

/**
 * This public class provides {@link WoD_RT_Client_Movement} list 
 *
 */
public class WoD_RT_Server_Engine_Movements_List {

	private ArrayList<WoD_RT_Client_Movement> movements = null;
	
	public WoD_RT_Server_Engine_Movements_List() {
		movements = new ArrayList<WoD_RT_Client_Movement>();
	}
	
	public ArrayList<WoD_RT_Client_Movement> getMovements() {
		return movements;
	}
	
	public void deleteMovements_BeforeTS(Long ts)
	{
		for (int position = 0; position < movements.size(); position++) {
			if (movements.get(position).getTimeStamp() < ts)
			{
				movements.remove(position);
			}
		}
	}
	
	public WoD_RT_Client_Movement get_Last_Movement_From_ID(int ID)
	{
		for (int position = movements.size() - 1; position > 0; position--) {
			if (movements.get(position).getID() == ID)
			{
				return movements.get(position);
			}
		}
		
		return null;
	}
	
	
	public ArrayList<WoD_RT_Client_Movement> get_Last_Movements_FromClients(ArrayList<WoD_Server_Client> clients)
	{
		ArrayList<WoD_RT_Client_Movement> clientMovements = new ArrayList<WoD_RT_Client_Movement>();
		
		for (WoD_Server_Client client : clients) {
			WoD_RT_Client_Movement clientMovement = get_Last_Movement_From_ID(client.getCliend_Id());
			if (clientMovement != null) 
			{
				clientMovements.add(clientMovement);
			}
		}
		
		return clientMovements;
	}
	
	
	public void add_WoD_RT_Client_Movement(WoD_RT_Client_Movement movement)
	{
		movements.add(movement);
	}
	
	public void remove_Old_Movements(Long ms_Ago)
	{
		Long ts_old = System.currentTimeMillis() - ms_Ago;
		
		for (int position = 0; position < movements.size(); position++) 
		{
			if (movements.get(position).getTimeStamp() < ts_old)
			{
				movements.remove(position);
			}	
		}
		
	}
	
}

